package com.wuliu.system.service.impl;

import java.math.BigDecimal;
import java.util.List;
import java.util.Objects;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.wuliu.common.constant.OrderConstants;
import com.wuliu.common.core.domain.entity.BasCust;
import com.wuliu.common.core.domain.entity.SysAppletUser;
import com.wuliu.common.core.domain.entity.SysDept;
import com.wuliu.common.exception.ServiceException;
import com.wuliu.common.utils.DateUtils;

import javax.annotation.Resource;

import com.wuliu.common.utils.SecurityUtils;
import com.wuliu.common.utils.StringUtils;
import com.wuliu.system.domain.bo.CalcOrderFeeBo;
import com.wuliu.system.domain.dto.applet.QryOrderFeeDto;
import com.wuliu.system.mapper.SysDeptMapper;
import com.wuliu.system.service.CommonService;
import org.springframework.stereotype.Service;
import com.wuliu.system.mapper.TspOrderPreMapper;
import com.wuliu.system.domain.TspOrderPre;
import com.wuliu.system.service.ITspOrderPreService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

/**
 * 预约单Service业务层处理
 *
 * @author realue
 * @date 2023-11-03
 */
@Service
public class TspOrderPreServiceImpl extends ServiceImpl<TspOrderPreMapper, TspOrderPre> implements ITspOrderPreService {
    @Resource
    private TspOrderPreMapper tspOrderPreMapper;
    @Resource
    private CommonService commonService;
    @Resource
    private SysDeptMapper sysDeptMapper;

    /**
     * 查询预约单
     *
     * @param id 预约单主键
     * @return 预约单
     */
    @Override
    public TspOrderPre selectTspOrderPreById(String id) {
        return tspOrderPreMapper.selectTspOrderPreById(id);
    }

    /**
     * 查询预约单列表
     *
     * @param tspOrderPre 预约单
     * @return 预约单
     */
    @Override
    public List<TspOrderPre> selectTspOrderPreList(TspOrderPre tspOrderPre) {
        return tspOrderPreMapper.selectTspOrderPreList(tspOrderPre);
    }

    /**
     * 新增预约单
     *
     * @param tspOrderPre 预约单
     * @return 结果
     */
    @Override
    public int insertTspOrderPre(TspOrderPre tspOrderPre) {
        tspOrderPre.setCreateTime(DateUtils.getNowDate());
        return tspOrderPreMapper.insertTspOrderPre(tspOrderPre);
    }

    /**
     * 修改预约单
     *
     * @param tspOrderPre 预约单
     * @return 结果
     */
    @Override
    public int updateTspOrderPre(TspOrderPre tspOrderPre) {
        tspOrderPre.setUpdateTime(DateUtils.getNowDate());
        return tspOrderPreMapper.updateTspOrderPre(tspOrderPre);
    }

    /**
     * 批量删除预约单
     *
     * @param ids 需要删除的预约单主键
     * @return 结果
     */
    @Override
    public int deleteTspOrderPreByIds(String[] ids) {
        return tspOrderPreMapper.deleteTspOrderPreByIds(ids);
    }

    /**
     * 删除预约单信息
     *
     * @param id 预约单主键
     * @return 结果
     */
    @Override
    public int deleteTspOrderPreById(String id) {
        return tspOrderPreMapper.deleteTspOrderPreById(id);
    }

    /**
     * 运费查询
     *
     * @param dto
     * @return
     */
    @Override
    public BigDecimal qryFee(QryOrderFeeDto dto) {
        SysAppletUser user = SecurityUtils.getAppletUser();
        SysDept sysDept = sysDeptMapper.selectOne(new LambdaQueryWrapper<SysDept>()
                .eq(SysDept::getSysAccount, user.getSysAccount())
                .eq(SysDept::getDeptNo, user.getDeptNo()));
        if (null == sysDept) {
            throw new ServiceException("用户所属站点信息不存在");
        }
        if (StringUtils.isAnyBlank(sysDept.getProvince(), sysDept.getCity())) {
            throw new ServiceException("请先完善用户省市信息");
        }
        if (Objects.isNull(dto.getCargoType())) {
            dto.setCargoType(OrderConstants.ORDER_CARGO_TYPE_1);
        }
        CalcOrderFeeBo calcOrderFeeBo = new CalcOrderFeeBo();
        BeanUtil.copyProperties(dto, calcOrderFeeBo);
        calcOrderFeeBo.setSrcProvince(sysDept.getProvince());
        calcOrderFeeBo.setSrcCity(sysDept.getCity());
        calcOrderFeeBo.setSysAccount(user.getSysAccount());
        return commonService.calcFee(calcOrderFeeBo);
    }
}
